//
//  CourseCenterItemViewController.m
//  ShowYork
//
//  Created by hxh on 2020/9/15.
//  Copyright © 2020 hxh. All rights reserved.
//

#import "LessonCenterItemViewController.h"
#import "LessonItemViewController.h"
#import "LessonDetailViewController.h"
#import "LessonSearchFlowLayout.h"
#import "LessonCategoryItemCell.h"
#import "PaddingLabel.h"
@interface LessonCenterItemViewController ()<LessonSearchFlowLayoutDelegate,LessonCategoryItemCellDelegate,UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic,assign)NSInteger itemType;
@property (nonatomic,strong)UICollectionView *collectionView;
@property (nonatomic,strong)NSMutableArray *sourceArray;
@property (nonatomic,strong)UILabel *remindLb;
@property (nonatomic,strong)UIView *remindView;
@end

@implementation LessonCenterItemViewController

- (NSMutableArray *)sourceArray{
    if (!_sourceArray) {
        _sourceArray = [NSMutableArray array];
    }
    return _sourceArray;
}

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        LessonSearchFlowLayout *layout = [LessonSearchFlowLayout new];
        layout.labelFont = [UIFont systemFontOfSize:14];
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        //自定义layout初始化
        layout.delegate = self;
        layout.lineSpacing = 8.0;
        layout.interitemSpacing = 15;
        layout.headerViewHeight = .0f;
        layout.footerViewHeight = .0f;
        layout.itemInset = UIEdgeInsetsMake(0, 10, 0, 10);

        _collectionView.dataSource = self;
        _collectionView.alwaysBounceVertical = YES;
        [_collectionView registerClass:[LessonCategoryItemCell class] forCellWithReuseIdentifier:@"Cell"];//cell
        _collectionView.backgroundColor = [UIColor whiteColor];

    }
    return _collectionView;
}
- (UIView *)remindView{
    if (!_remindView) {
        _remindView = [[UIView alloc]init];
        _remindView.backgroundColor = RGBA(248, 248, 248, 1);
        _remindView.layer.cornerRadius = 10;
        _remindView.layer.masksToBounds = YES;
    }
    return _remindView;
}

- (UILabel *)remindLb{
    if (!_remindLb) {
        _remindLb = [[UILabel alloc]init];
       //indLb.edgeInsets = UIEdgeInsetsMake(16, 16, 16, 16);
        _remindLb.textColor = C102;
        _remindLb.font = font(14);
        _remindLb.numberOfLines = 0;
    }
    return _remindLb;
}

- (instancetype)initWithItemType:(NSInteger)itemType{
    if (self = [super init]) {
        self.itemType = itemType;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.isHideNavBar = YES;
    
    [self.view addSubview:self.collectionView];
    [self.view addSubview:self.remindView];
    [self.remindView addSubview:self.remindLb];
    
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        if (@available(iOS 11,*)) {
            make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop);
            make.left.equalTo(self.view.mas_safeAreaLayoutGuideLeft);
            make.right.equalTo(self.view.mas_safeAreaLayoutGuideRight);
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
        }else{
            make.top.equalTo(self.view.mas_top);
            make.left.equalTo(self.view.mas_left);
            make.right.equalTo(self.view.mas_right);
            make.bottom.equalTo(self.view.mas_bottom);
        }
    }];
    

    if (self.itemType == 1) {
        self.remindLb.text = @"语文微课共60讲，课程设计涵盖浙江卷和全国卷所有考点，将常考的重难点和预测的新考点结合，针对考生容易出现的错误给予辅导，再配以实用性极强的练习。既可以作为高三阶段系统复习，也可以查漏补缺选择性地进行学习。";
    }
    if (self.itemType == 2) {
        self.remindLb.text = @"数学微课以“小菜一碟——多变量问题求解策略”为开篇，继以60讲构成。内容涵盖高中数学的考点、热点与难点。每节微课有“内容简介，例题讲解，课堂小结，微练习”等环节，帮助学生夯实基础，有效提分。";
    }
    if (self.itemType == 3) {
        self.remindLb.text = @"英语微课由九大板块（共60讲）构成，课程设计紧扣考纲的考查要点，每节微课包含“‘痛点’我知道”、“专家如何考”、“得分有锦囊”、“模拟练兵场”或“小试牛刀”等多个环节，以帮助学生提升解题能力。";
    }
    
    [self.remindView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.view).offset(-(GK_SAVEAREA_BTM + 16));
        make.left.mas_equalTo(self.view).offset(16);
        make.right.mas_equalTo(self.view).offset(-16);
    }];
    [self.remindLb mas_makeConstraints:^(MASConstraintMaker *make){
        make.bottom.mas_equalTo(self.remindView).offset(-16);
        make.top.left.mas_equalTo(self.remindView).offset(16);
        make.right.mas_equalTo(self.remindView).offset(-16);
    }];
    
    
    
    
    [self loadCategorySource];
    
    // Do any additional setup after loading the view.
}

#pragma mark - CourseCategoryItemCellDelegte
- (void)showCategoryDataWithCurrentModel:(LessonCategoryModel *)model{

    LessonItemViewController *itemVC = [[LessonItemViewController alloc]initWithItemType:XHCOURSEITEMALL categoryId:model.id.intValue subjectId:self.itemType + 1];
    itemVC.title = model.categoryName;
    [self.navigationController pushViewController:itemVC animated:YES];
}
#pragma mark - 网络请求
//
- (void)loadCategorySource{
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"md5"] = [RPAppManager md5StrWithApi:GetCategories];
    param[@"subjectId"] = @(self.itemType + 1);
    param[@"pageSize"] = @(9999);
    param[@"pageIndex"] = @(1);
    [[RPNetworkExtensionTool sharedTool]requestPOST:kGET_REQUEST_URL(GetCategories) parames:param success:^(id responseObj) {
        if (![responseObj[RESPONSE][@"list"] isKindOfClass:[NSNull class]]) {
            self.sourceArray = [LessonCategoryModel mj_objectArrayWithKeyValuesArray:responseObj[RESPONSE][@"list"]];
        }
        LessonCategoryModel *allModel = [[LessonCategoryModel alloc]init];
        allModel.id = @(0);
        allModel.categoryName = @"全部";
        [self.sourceArray insertObject:allModel atIndex:0];
        [self.collectionView reloadData];
    } failure:^(id error) {

    }];
}

#pragma mark - <UICollectionViewDataSource>
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.sourceArray.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    LessonCategoryItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    [cell setModel:self.sourceArray[indexPath.row]];
    cell.delegate = self;
    return cell;
}


#pragma mark-  用协议传回 item 的内容,用于计算 item 宽度
- (NSString *)collectionViewItemSizeWithIndexPath:(NSIndexPath *)indexPath{
    LessonCategoryModel *model = self.sourceArray[indexPath.row];
    return model.categoryName;
}

@end
